﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.Models;
using System.Data.SqlClient;
using System.Data;

namespace PBOffice.DAL
{
    public class SysFunService
    {
        public List<SysFun> GetAllSysFun()
        {
            string sql = "select * from SysFun order by ParentNodeId,DisplayOrder ";
            List<SysFun> list = new List<SysFun>();
            using (SqlDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    SysFun sf = new SysFun();
                    sf.NodeId = Convert.ToInt32(dr["NodeId"]);
                    sf.DisplayName = dr["DisplayName"].ToString();
                    sf.NodeURL = dr["NodeURL"].ToString();
                    sf.DisplayOrder = Convert.ToInt32(dr["DisplayOrder"]);
                    sf.ParentNodeId = Convert.ToInt32(dr["ParentNodeId"]);
                    list.Add(sf);
                }
            }
            return list;
        }

        public List<SysFun> GetAllSysFunByParId(int id)
        {
            string sql = string.Format("select * from SysFun where ParentNodeId={0}", id);
            List<SysFun> list = new List<SysFun>();
            using (SqlDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    SysFun sf = new SysFun();
                    sf.NodeId = Convert.ToInt32(dr["NodeId"]);
                    sf.DisplayName = dr["DisplayName"].ToString();
                    sf.NodeURL = dr["NodeURL"].ToString();
                    sf.DisplayOrder = Convert.ToInt32(dr["DisplayOrder"]);
                    sf.ParentNodeId = Convert.ToInt32(dr["ParentNodeId"]);
                    list.Add(sf);
                }
            }
            return list;
        }
        public SysFun GetSysFunByNodeId(int id)
        {
            string sql = string.Format("select * from SysFun where NodeId={0}", id);
            SysFun sf = new SysFun();
            using (SqlDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {

                    sf.NodeId = Convert.ToInt32(dr["NodeId"]);
                    sf.DisplayName = dr["DisplayName"].ToString();
                    sf.NodeURL = dr["NodeURL"].ToString();
                    sf.DisplayOrder = Convert.ToInt32(dr["DisplayOrder"]);
                    sf.ParentNodeId = Convert.ToInt32(dr["ParentNodeId"]);

                }
            }
            return sf;
        }
        public void UpdateSysFun(SysFun sys)
        {
            string sql = "update [SysFun] set DisplayName=@DisplayName, NodeURL=NodeURL,DisplayOrder=@DisplayOrder,ParentNodeId=@ParentNodeId"
            + " where  NodeId=@NodeId";
            SqlParameter[] spa = {
                                     new SqlParameter("@NodeId",sys.NodeId),
                                     new SqlParameter("@DisplayName",sys.DisplayName),
                                     new SqlParameter("@NodeURL",sys.NodeURL),
                                     new SqlParameter("@DisplayOrder",sys.DisplayOrder),
                                     new SqlParameter("@ParentNodeId",sys.ParentNodeId)

                                 };
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql, spa);
        }
        public SysFun GetSysFunByOrderIdAndParentId(int orderId, int parentId)
        {
            string sql = string.Format("select * from SysFun where DisplayOrder={0} and ParentNodeId={1}", orderId, parentId);
            SysFun sf = new SysFun();
            using (SqlDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {

                    sf.NodeId = Convert.ToInt32(dr["NodeId"]);
                    sf.DisplayName = dr["DisplayName"].ToString();
                    sf.NodeURL = dr["NodeURL"].ToString();
                    sf.DisplayOrder = Convert.ToInt32(dr["DisplayOrder"]);
                    sf.ParentNodeId = Convert.ToInt32(dr["ParentNodeId"]);

                }
            }
            return sf;
        }

    }
}
